/**
 * @description 目前支持配置的就index.js的那些 ['prizeConfig', 'Questions', 'topIssues', 'skinAnalysis',
'Cosmetics', 'Strategy','Product','IndexCompareText','IndexCompareText']

 */

import Vue from 'vue'
import { getStrategyList, getList, doEdit } from '@/api/configration'

const state = () => ({
  strategyList: [],
  strategyRow: {},
  productList: [],
  solutionList: [],
  topIssuesList: [],
  indexCompareTextList: [],
  skinAnalysisList: {},
})
const getters = {
  strategyList: (state) => state.strategyList,
  strategyRow: (state) => state.strategyRow,
  productList: (state) => state.productList,
  solutionList: (state) => state.solutionList,
  topIssuesList: (state) => state.topIssuesList,
  skinAnalysisList: (state) => state.skinAnalysisList,
  indexCompareTextList: (state) => state.indexCompareTextList,
}
const mutations = {
  setStrategyList(state, strategyList) {
    state.strategyList = strategyList
  },
  setStrategyRow(state, strategyRow) {
    state.strategyRow = strategyRow
  },
  setProductList(state, productList) {
    state.productList = productList
  },
  setSolutionList(state, solutionList) {
    state.solutionList = solutionList
  },
  setTopIssuesList(state, topIssuesList) {
    state.topIssuesList = topIssuesList
  },
  setSkinAnalysisList(state, skinAnalysisList) {
    state.skinAnalysisList = skinAnalysisList
  },
  setIndexCompareTextList(state, indexCompareTextList) {
    state.indexCompareTextList = indexCompareTextList
  },
}
const actions = {
  async getStrategyList({ commit }) {
    const { data } = await getStrategyList()
    if (!data) {
      Vue.prototype.$baseMessage('获取护肤配置数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && Array.isArray(data)) {
      commit('setStrategyList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async getProductList({ commit }) {
    const { data, error } = await getList('Product')
    if (!data || error) {
      Vue.prototype.$baseMessage('获取产品推荐数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && Array.isArray(data)) {
      commit('setProductList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async getSolutionList({ commit }) {
    const { data, error } = await getList('Solution')
    if (!data || error) {
      Vue.prototype.$baseMessage('获取解决方案文案配置数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && Array.isArray(data)) {
      commit('setSolutionList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async getIndexCompareTextList({ commit }) {
    const { data, error } = await getList('IndexCompareText')
    if (!data || error) {
      Vue.prototype.$baseMessage('获取解决方案数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && Array.isArray(data)) {
      commit('setIndexCompareTextList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async getTopIssuesList({ commit }) {
    const { data, error } = await getList('topIssues')
    if (!data || error) {
      Vue.prototype.$baseMessage('获取解决方案数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && Array.isArray(data)) {
      commit('setTopIssuesList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async getSkinAnalysisList({ commit }) {
    const { data, error } = await getList('skinAnalysis')
    if (!data || error) {
      Vue.prototype.$baseMessage('获取相关肌肤指标文案配置数据失败...', 'error')
      return false
    }
    // let { permissions, username, avatar } = data
    if (data && typeof data === 'object') {
      commit('setSkinAnalysisList', data)
      // commit('setUsername', username)
      // commit('setAvatar', avatar)
      return data
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
}
export default { state, getters, mutations, actions }
